 let base_url = 'http://api.leiwei.fun/index.php'
 // base_url = '/api'
// base_url = '/baseUrlTest/apis'
export let http_url = base_url
import Vue from "vue"
import {
	msg,
	to
} from "./uilt/index.js"
import {
	parse
} from "qs"
// import {
// 	toUrl
// } from "./url"
let http = (url, method, data, header) => {
	uni.showLoading({
		title: "加载中"
	});
	return new Promise((resolve, reject) => {
		// let userData = uni.getStorageSync('userData');
		uni.request({
			url: base_url + url,
			method,
			header: header||{
				'content-type': "application/json",
				// satoken: userData.satoken,
				
			},
			data,
			success(res) {
				uni.hideLoading();
				res = res.data
				if (res.code === "1") {
					resolve(res)
					return
				} else if (res.code === 400101) {
					uni.redirectTo({url:"/pages/login/login"})
					
					return
				} else if (res.code === 400102) {
					msg("登录失败，请联系管理员")
					return
				}
				let error 
				try{
					error = res.errors.toString()
				}catch(e){
					//TODO handle the exception
					error = res.msg
				}
				resolve(res)
				msg(error)
				// return (msg(res.msg))
			},
			fail(err) {
				reject(err)
			},
			complete() {
				uni.hideLoading();
			}
		})
	})
}
// 文件上传
export let upload = (url,filePath)=>{
	console.log(url,filePath,"88888889999999");
	return new Promise((resolve,rejct)=>{
		uni.uploadFile({
			header:{},
			url:base_url+url,
			filePath:filePath,
			name:'image',
			success(res) {
				resolve(res)
			},
			fail(err) {
				rejct(err)
			}
		})
	})
}
export let get = function(url, data = {}) {
	return http(url, 'get', parse(data))
}
export let post = (url, data,header) => {
	let userInfo = uni.getStorageSync('userInfo')||{}
	data.token = userInfo.token
	return http(url, "post", data,header)
}

